package com.springapp.mvc.controller;

import com.springapp.mvc.dao.bean.CallRecords;
import com.springapp.mvc.params.CallHistoryQueryParam;
import com.springapp.mvc.params.CallRecordsParam;
import com.springapp.mvc.resultBean.BaseResult;
import com.springapp.mvc.resultBean.CommonDatatableBean;
import com.springapp.mvc.resultBean.OperationDeleteBean;
import com.springapp.mvc.resultBean.ResultCode;
import com.springapp.mvc.service.Interface.CallRecordsService;
import com.springapp.mvc.service.Interface.OperationService;
import com.springapp.mvc.websocket.SocketMessageBean;
import com.springapp.mvc.websocket.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qinyy on 2018/8/14.
 */
@Controller
@RequestMapping("/callRecords")
public class CallRecordsController
{
    @Autowired(required = false)
    CallRecordsService mCallRecordsService;

    @Autowired(required = false)
    OperationService mOperationService;

/*    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "insert")
    public BaseResult insertTest(HttpServletRequest request)
    {
        return null;
    }*/

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "query")
    public BaseResult queryCallHistory(CallHistoryQueryParam param)
    {
        if(param == null)
            return null;
        BaseResult result = new BaseResult();
        CommonDatatableBean tableBean = new CommonDatatableBean();
        tableBean.setDraw(0);
        int totalAmount = mCallRecordsService.getTotalCount();
        tableBean.setRecordsTotal(totalAmount);
        tableBean.setRecordsFiltered(totalAmount);
        int limit = param.getStart();
        List<Object> list = new ArrayList<Object>();
        String sortType = param.getSortType() == 0?"ASC":"DESC";
        List<CallRecords> records = mCallRecordsService.queryCallRecords(limit, param.getOffset(), param.getSortPro(), sortType);
        if(records != null && records.size() > 0)
        {
            for(CallRecords record:records)
            {
                list.add(record);
            }
        }
        tableBean.setData(list);
        result.setData(tableBean);
        result.setResult(ResultCode.SUCCESS);
        return result;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "insert")
    public BaseResult insertCallRecords(@RequestBody CallRecordsParam param)
    {
        BaseResult result = new BaseResult();

        if(param == null || param.getCallRecordsList() == null || param.getCallRecordsList().size() == 0)
        {
            result.setMessage("insert records are null");
            result.setCode("1");
         //   result.setData(new Object());
        }
        try
        {
            mCallRecordsService.insertCallRecords(param.getCallRecordsList());
            result.setCode("0");
        //    result.setData(new Object());
        }
        catch (Exception e)
        {
            result.setMessage(e.getMessage());
            result.setCode("1");
        //    result.setData(new Object());
        }

        // 命令更新成已上传
        mOperationService.changeOperationStatus(param.getOperationid(), true);

        // 通知管理平台一条operation已经被执行,有新的通话记录插入
        SocketMessageBean sockBean = new SocketMessageBean();
        sockBean.setType("1");
        sockBean.setData(mOperationService.queryOperationById(param.getOperationid()));

        SocketMessageBean sockBean2 = new SocketMessageBean();
        sockBean2.setType("3");
        try {
           // WebSocketService.sendObjectToAll(sockBean);
            WebSocketService.sendObjectToAll(sockBean2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "delete")
    public BaseResult deleteRecord(long callId)
    {
        BaseResult result = new BaseResult();
        CallRecords bean = new CallRecords();
        int ret = mCallRecordsService.deleteRecord(callId);
        bean.setRecordsid(callId);
        result.setData(bean);
        result.setCode("0");
        return result;
    }
}
